package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_dialog

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener
import com.bp.hearthealth.pulse.lnapps.databinding.CalendarDialogLayoutBgBinding
import com.isnoam.calendar.bean.DateInfo

class CalendarDialog(context: Activity) {
    private lateinit var binding: CalendarDialogLayoutBgBinding
    private lateinit var onCliclLisener: OnDateRangeSelectedListener
    private var startDate: DateInfo? = null
    private var endDate: DateInfo? = null
    private lateinit var dialog: Dialog
    private val activity: Activity = context

    @SuppressLint("SetTextI18n")
    fun showDialog() {
        dialog = Dialog(activity, R.style.My_Dialog_FullScreen)
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(activity), R.layout.calendar_dialog_layout_bg, null, false
        )
        binding.root.let { dialog.setContentView(it) }//核心代码
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        val lp = dialog.window?.attributes
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT

        lp?.alpha = 1f
        dialog.window?.attributes = lp
        if (activity.isFinishing) {
            return
        }
        dialog.show()
        binding.calendarView.setOnDateRangeSelectedListener { _, // 日历控件
                                                              _, // 选中的日期
                                                              startDate: DateInfo, // 开始日期
                                                              endDate: DateInfo? -> // 结束日期
            this.endDate = endDate
            this.startDate = startDate
            if (endDate == null) {
                //选择某一天
                binding.tvRangeTime.text = startDate.formatAsMMMdd()
                LogUtil.i("选择时间：${startDate.formatAsMMMdd()}")
            } else {
                //选择的是范围
                binding.tvRangeTime.text = "${startDate.formatAsMMMdd()}-${endDate.formatAsMMMdd()}"
                LogUtil.i("时间范围：${startDate.formatAsMMMdd()}-${endDate.formatAsMMMdd()}")
            }
        }
        binding.tvCancel.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                dialog.dismiss()
            }
        })
        binding.tvOk.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                dialog.dismiss()
                if (startDate == null) {
                    return
                }
                onCliclLisener.setOnDateRangeSelectedListener(startDate!!, endDate)
            }
        })
    }

    fun dismiss() {
        dialog.dismiss()
    }

    fun setOnBtnClickLisener(clickListener: OnDateRangeSelectedListener) {
        this.onCliclLisener = clickListener
    }

    interface OnDateRangeSelectedListener {
        fun setOnDateRangeSelectedListener(startDate: DateInfo, endDate: DateInfo?)
    }
}
